#include "PowerSlider.h"
#include "Factory.h"
#include "TextureManager.h"
#include "PlayState.h"
#include "Catapult.h"
#include "Timer.h"


PowerSlider::PowerSlider()
{
	m_pointerPos.Y = 0;
	m_pointerSize = 0.1f;
	m_sliderSpeed = 2.8f;
	m_pointerUp = true;
	mp_catapult = ThePlayState::Instance()->GetCatapultAddress();
	TheEventPoller::Instance()->AddHandler(this);
}

PowerSlider::~PowerSlider()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void PowerSlider::Draw()
{

	if(mp_catapult->GetIsFiring() || mp_catapult->GetHasFired())
	{
		glPushMatrix();

			//PowerSlider
			TheTextureManager::Instance()->DrawTexture(m_texPath);

			glBegin(GL_QUADS);
			//counter clockwise winding 
			glTexCoord2f(0.0f, 1.0f); //coord for points
			glVertex3f((m_pos.X - 1.0f), (m_pos.Y - 1.0f), 0); //UVs
		

			glTexCoord2f(1.0f, 1.0f); //coord for points
			glVertex3f((m_pos.X - 1.0f) + m_dimensions.X, (m_pos.Y - 1.0f), 0); //UVs
			

			glTexCoord2f(1.0f, 0.0f); //coord for points
			glVertex3f((m_pos.X - 1.0f) + m_dimensions.X, (m_pos.Y - 1.0f) + m_dimensions.Y, 0); //UVs
			

			glTexCoord2f(0.0f, 0.0f); //coord for points
			glVertex3f((m_pos.X - 1.0f), (m_pos.Y - 1.0f) + m_dimensions.Y, 0); //UVs			

			glEnd();
			//Pointer
			TheTextureManager::Instance()->DrawTexture(m_slidingPointerTexPath);

			glBegin(GL_QUADS);
			
			glTexCoord2f(0.0f, 1.0f); 
			glVertex3f((m_pointerPos.X - 0.7f), (m_pointerPos.Y - 1.0f), 0); //coord for points
		
			glTexCoord2f(1.0f, 1.0f); //coord for points
			glVertex3f((m_pointerPos.X - 0.7f) + m_dimensions.X + 0.1f, (m_pointerPos.Y - 1.0f), 0); //coord for pointss
			
			glTexCoord2f(1.0f, 0.0f); //UV
			glVertex3f((m_pointerPos.X - 0.7f) + m_dimensions.X + 0.1f, (m_pointerPos.Y - 1.0f) + m_dimensions.Y/8, 0); //coord for points

			glTexCoord2f(0.0f, 0.0f); //UV
			glVertex3f((m_pointerPos.X - 0.7f), (m_pointerPos.Y - 1.0f) + m_dimensions.Y/8, 0); //coord for points	
			
			glEnd();

		glPopMatrix();
	}
}

void PowerSlider::Update()
{
	if(mp_catapult->GetIsFiring())
	{
		if(m_pointerUp)
		{
			m_pointerPos.Y += m_sliderSpeed * TheTimer::Instance()->GetDT();
			if(m_pointerPos.Y > m_dimensions.Y + 0.5f)
			{
				m_pointerUp = false;
			}
		}

		else
		{
			m_pointerPos.Y -= m_sliderSpeed * TheTimer::Instance()->GetDT();
			if(m_pointerPos.Y <= m_pos.Y)
			{
				m_pointerUp = true;
			}
		}
	}
}

void PowerSlider::OnKeyboardEvent(const SDL_KeyboardEvent&  key)
{
	if(mp_catapult->GetIsFiring() && mp_catapult->GetIsReadyFire())
	{
		if(key.keysym.sym == SDLK_SPACE && key.state == SDL_PRESSED)
		{
			float pow = (m_pointerPos.Y/ (m_pointerPos.Y + m_dimensions.Y + 0.5f)) * 12.5f;
			mp_catapult->SetFiringSpeed(pow);
			mp_catapult->HasFired();
		}
	}

}

void PowerSlider::PointerPos()
{
	m_pointerPos.X = m_pos.X - 0.2f;
	m_pointerPos.Y = m_pos.Y;
}

bool PowerSlider::Load(File* const f)
{

	//Format is:
	// texture path slider
	// texture path pointer
	// xy pos relative to coord system with model and projection set to identity
	// And pos coords in between -1 and 1 x and y
	// xy size
	if(!f->GetString(&m_texPath))
	{
		ReportError("Could not load PowerSlider texpath.");
	}
	if(!f->GetString(&m_slidingPointerTexPath))
	{
		ReportError("Could not load PowerSlider texpath.");
	}
	if(!f->GetFloat(&m_pos.X))
	{
		ReportError("Could not load PowerSlider xPos.");
	}
	if(!f->GetFloat(&m_pos.Y))
	{
		ReportError("Could not load PowerSlider yPos.");
	}
	if(!f->GetFloat(&m_dimensions.X))
	{
		ReportError("Could not load PowerSlider ySize.");
	}
	if(!f->GetFloat(&m_dimensions.Y))
	{
		ReportError("Could not load PowerSlider ySize.");
	}
	if(!f->GetFloat(&m_pointerSize))
	{
		ReportError("Could not load PowerSlider ySize.");
	}

	m_pointerPos.X = m_pos.X + m_dimensions.X;
	m_pointerPos.Y = m_pos.Y;

	return true;
}

void PowerSlider::SetPointerTexPath(const std::string& texPath)
{
	m_slidingPointerTexPath = texPath;
}

const char* PowerSlider::GetTypeName()
{
	return "PowerSlider";
}

GUIObject* CreatePowerSlider()
{
	return new PowerSlider;
}

static bool powSlid = TheGUIObjectFactory::Instance()->AddToMap("PowerSlider", CreatePowerSlider);

